﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Simple.Services.System;

namespace Simple.Services
{
    public class LecturerService : BusinessBaseService
    {
        private readonly SimpleDbContext _context;
        public LecturerService(SimpleDbContext context,
            ISimpleService services) : base(services)
        {
            _context = context;
        }

        public async Task<List<LecturerModel>> GetAsync()
        {
            var lstLecturer = await (await OrganizationFilter(_context.Set<Lecturer>())).ToListAsync();
            return MapperHelper.Map<List<LecturerModel>>(lstLecturer);
        }

        public async Task<PageResultModel<LecturerModel>> GetPageAsync(LecturerPageInputModel input)
        {
            var result = new PageResultModel<LecturerModel>();
            var query = await OrganizationFilter(_context.Set<Lecturer>().AsQueryable());

            // 根据条件查询
            if (!string.IsNullOrEmpty(input.Name))
            {
                query = query.Where(u => u.Name.Contains(input.Name));
            }
            if (input.LecturerType.HasValue)
            {
                query = query.Where(u => u.LecturerType == input.LecturerType);
            }

            // 获取总数量
            result.TotalRows = await query.CountAsync();

            // 分页查询
            query = query.OrderBy(u => u.CreatedTime).Page(input.PageNo, input.PageSize);
            var lstLecturer = await query.ToListAsync();
            var lstLecturerDto = MapperHelper.Map<List<LecturerModel>>(lstLecturer);

            result.Rows = lstLecturerDto;

            result.SetPage(input);
            result.CountTotalPage();

            return result;
        }

        public async Task<int> AddAsync(LecturerInputModel model)
        {
            var lecturer = MapperHelper.Map<Lecturer>(model);
            await _context.AddAsync(lecturer);

            return await _context.SaveChangesAsync();
        }

        public async Task<int> UpdateAsync(LecturerInputModel model)
        {

            var lecturer = await _context.Set<Lecturer>()
                .Where(u => model.Id == u.Id)
                .FirstOrDefaultAsync();

            if (lecturer == null)
            {
                throw AppResultException.Status404NotFound("找不到讲师，更新失败");
            }
            MapperHelper.Map(model, lecturer);

            _context.Update(lecturer);
            int ret = await _context.SaveChangesAsync();

            if (ret == 0)
            {
                throw AppResultException.Status200OK("更新记录数为0");
            }
            return ret;
        }

        public async Task<int> DeleteAsync(IEnumerable<Guid> ids)
        {
            var lstLecturer = await _context.Set<Lecturer>()
                .Where(u => ids.Contains(u.Id))
                .ToListAsync();

            _context.RemoveRange(lstLecturer);
            return await _context.SaveChangesAsync();
        }
    }


}
